<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午11:12
 */

namespace backend\controllers;
use Yii;
use common\models\BannerModel;
use common\models\HouseModel;
use common\logic\BannerLogic;
use backend\models\SearchBannerModel;
use yii\web\NotFoundHttpException;
use yii\helpers\ArrayHelper;

class BannerController extends BaseController
{
    public function actionIndex()
    {
        $searchModel = new SearchBannerModel();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->sort = false;
		
//		 exit('$dataProvider='. json_encode($dataProvider));
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    
    public function actionCreate()
    {
        $model = new BannerModel();
        $model->region = 0;
        $model->banner = 0;
        $model->houseid = 0;
        //$model->creater = Yii::$app->user->getId();

        $post = Yii::$app->request->post();

		$house = new HouseModel();
        if ($model->load($post) && BannerLogic::instance()->BannnerSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'house' => $house
            ]);
        }
    }
	
	public function actionCreatetest()
    {
        $model = new BannerModel();
        $model->region = 0;
        $model->banner = 0;
        $model->houseid = 0;
        //$model->creater = Yii::$app->user->getId();

        $post = Yii::$app->request->post();


        if ($model->load($post) && BannerLogic::instance()->BannnerSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->renderAjax('createtest', [
                'model' => $model,
            ]);
        }
    }
    
    /**
     * Deletes an existing HouseModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        BannerModel::deleteAll(['id'=>$id]);

        return $this->redirect(['index']);
    }
    
    /**
     * Finds the HouseModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return HouseModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = BannerModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
		$house = HouseModel::findOne($model['houseid']);
		
//		echo('aabbcc='.$model['houseid']);
//		exit(json_encode($house));
		
		if(!$house){
			$house = ['house_name'=>''];
		}
		
        $post = Yii::$app->request->post();
        unset($post['BannerModel']['banner']);
        //unset($post['BannerModel']['banner'],$post['HouseModel']['adv']);
		
        if ($model->load($post) && BannerLogic::instance()->houseSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('update', [
                'model' => $model,
                'house'	=> $house
            ]);
        }
    }
}